﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace RGPWEB.admin.MCode
{
    public partial class CreateMcode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonRule_Click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = ObjectDataSource1.ID;
   
           // Image1.ImageUrl ="~/inc/images/MRuleImg/diode.png";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView2.DataSourceID = ObjectDataSource2.ID;
        }

        protected void ButtonCreateMcode_Click(object sender, EventArgs e)
        {
            TextBox tbCT0 = GridView1.FooterRow.FindControl("tbCT0") as TextBox;
            TextBox tbCT1 = GridView1.FooterRow.FindControl("tbCT1") as TextBox;
            TextBox tbCT2 = GridView1.FooterRow.FindControl("tbCT2") as TextBox;
            TextBox tbCT3 = GridView1.FooterRow.FindControl("tbCT3") as TextBox;
            TextBox tbCT4 = GridView1.FooterRow.FindControl("tbCT4") as TextBox;
            TextBox tbCT5 = GridView1.FooterRow.FindControl("tbCT5") as TextBox;
            TextBox tbCT6 = GridView1.FooterRow.FindControl("tbCT6") as TextBox;
            TextBox tbCT7 = GridView1.FooterRow.FindControl("tbCT7") as TextBox;
            TextBox tbCT8 = GridView1.FooterRow.FindControl("tbCT8") as TextBox;
            TextBox tbCT9 = GridView1.FooterRow.FindControl("tbCT9") as TextBox;

            Label lbCT0 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT0")) as Label;
            Label lbCT1 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT1")) as Label;
            Label lbCT2 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT2")) as Label;
            Label lbCT3 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT3")) as Label;
            Label lbCT4 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT4")) as Label;
            Label lbCT5 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT5")) as Label;
            Label lbCT6 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT6")) as Label;
            Label lbCT7 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT7")) as Label;
            Label lbCT8 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT8")) as Label;
            Label lbCT9 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT9")) as Label;

           string strTemp3 = GridView1.Rows[GridView1.Rows.Count - 1].Cells[1].Text;

           tbMcodeFinal.Text = strTemp3 + "." + tbCT2.Text + tbCT3.Text + tbCT4.Text + tbCT5.Text + tbCT6.Text
                               + tbCT7.Text + tbCT8.Text + tbCT9.Text;

          
            if(tbMcodeFinal.Text.Length>= (Convert.ToInt16(tbMcodeLength.Text)+2))
            
            tbMcodeFinal.Text = tbMcodeFinal.Text.Substring(0, Convert.ToInt16(tbMcodeLength.Text)+2);

          //  if (tbCT0==null)


           




        }


        protected  string ZeroFill(int xx)
        {

            switch (xx)
            {
              case  1: return ("0");
              case  2: return ("00");
              case 3:  return ("000");
              case 4:  return ("0000");
              case 5:  return ("00000");
              case 6:  return ("000000");
              case 7:  return ("0000000");
              case 8:  return ("00000000");
              case 9:  return ("000000000");
              case 10: return ("0000000000");
              case 11: return ("00000000000");
              default: return ("0");

            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Label lbCT0 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT0")) as Label;
            Label lbCT1 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT1")) as Label;
            Label lbCT2 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT2")) as Label;
            Label lbCT3 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT3")) as Label;
            Label lbCT4 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT4")) as Label;
            Label lbCT5 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT5")) as Label;
            Label lbCT6 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT6")) as Label;
            Label lbCT7 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT7")) as Label;
            Label lbCT8 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT8")) as Label;
            Label lbCT9 = (GridView1.Rows[GridView1.Rows.Count - 1].FindControl("lbCT9")) as Label;

            TextBox tbCT0 = GridView1.FooterRow.FindControl("tbCT0") as TextBox;
            TextBox tbCT1 = GridView1.FooterRow.FindControl("tbCT1") as TextBox;
            TextBox tbCT2 = GridView1.FooterRow.FindControl("tbCT2") as TextBox;
            TextBox tbCT3 = GridView1.FooterRow.FindControl("tbCT3") as TextBox;
            TextBox tbCT4 = GridView1.FooterRow.FindControl("tbCT4") as TextBox;
            TextBox tbCT5 = GridView1.FooterRow.FindControl("tbCT5") as TextBox;
            TextBox tbCT6 = GridView1.FooterRow.FindControl("tbCT6") as TextBox;
            TextBox tbCT7 = GridView1.FooterRow.FindControl("tbCT7") as TextBox;
            TextBox tbCT8 = GridView1.FooterRow.FindControl("tbCT8") as TextBox;
            TextBox tbCT9 = GridView1.FooterRow.FindControl("tbCT9") as TextBox;

            string strTemp1 = GridView2.Rows[e.NewSelectedIndex].Cells[2].Text;
            string strTemp2 = GridView2.Rows[e.NewSelectedIndex].Cells[3].Text;
            string strTemp3 = GridView1.Rows[GridView1.Rows.Count - 1].Cells[1].Text;


          //  string[] strTempG2={"","","","","",""};
            string _strTempG2="";


            
 
            for(int i=2;i<6;i++)
            {
                _strTempG2 = _strTempG2 +" "+"["+GridView2.Rows[e.NewSelectedIndex].Cells[i].Text+"]";
            }





            if ((tbCT0.Text == "") && (strTemp3.Length >= 2)) tbCT0.Text = strTemp3.Substring(0, 2);
            if ((tbCT1.Text == "") && (strTemp3.Length >= 5)) tbCT1.Text = strTemp3.Substring(3, 2);
           if (tbCT2.Text == "") tbCT2.Text = ZeroFill(lbCT2.Text.Length);
           if (tbCT3.Text == "") tbCT3.Text = ZeroFill(lbCT3.Text.Length);
           if (tbCT4.Text == "") tbCT4.Text = ZeroFill(lbCT4.Text.Length);
           if (tbCT5.Text == "") tbCT5.Text = ZeroFill(lbCT5.Text.Length);
           if (tbCT6.Text == "") tbCT6.Text = ZeroFill(lbCT6.Text.Length);
           if (tbCT7.Text == "") tbCT7.Text = ZeroFill(lbCT7.Text.Length);
           if (tbCT8.Text == "") tbCT8.Text = ZeroFill(lbCT8.Text.Length);
           if (tbCT9.Text == "") tbCT9.Text = ZeroFill(lbCT9.Text.Length);

          //  Response.Write("---" + strTemp1 + "--"+ strTemp2);

            if ((lbCT0.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text =tbMcodeNote.Text + _strTempG2;
                

                tbCT0.Text =  strTemp2;
                return;
            }

            if ((lbCT1.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                
                tbCT1.Text = strTemp2;
                return;
            }

            if ((lbCT2.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                
                tbCT2.Text = strTemp2;
                return;
            }

            if ((lbCT3.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT3.Text = strTemp2;
                return;
            }

            if ((lbCT4.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT4.Text = strTemp2;
                return;
            }

            if ((lbCT5.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT5.Text = strTemp2;
                return;
            }

            if ((lbCT6.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT6.Text = strTemp2;
                return;
            }

            if ((lbCT7.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT7.Text = strTemp2;
                return;
            }

            if ((lbCT8.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT8.Text = strTemp2;
                return;
            }

            if ((lbCT9.Text == strTemp1) && (strTemp1 != null))
            {
                tbMcodeNote.Text = tbMcodeNote.Text + "\n" + _strTempG2;
                tbCT9.Text = strTemp2;
                return;
            }
          
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // tbMcodeFinal.Attributes.Add("OnClick", "this.value = '';");

            //foreach (Control con in this.Controls[1].Controls)
            //{
            //    if (con is TextBox)
            //    {
            //        ((TextBox)con).Text = " ";
            //    }
            //}

           Image1.ImageUrl =   "~/inc/images/MRuleImg/"+DropDownList1.SelectedItem.Value.Replace('.','_')+".png";
             


        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            //tbMcodeFinal.Attributes.Add("OnClick", "this.value = '';");

            this.tbMcodeNote.Text = "";
        }
    }
}
